import 'package:flutter/material.dart';

import '../tools/KaL_Config.dart';
import '../tools/KaL_drawNumbersView.dart';
import '../tools/models/KL_keralaLottoModel.dart';
import 'views/Kal_resultDetailListCell.dart';

class KalResultdetailpage extends StatefulWidget {
  KalResultdetailpage({super.key, required this.prizes});
  List prizes = [];

  @override
  State<KalResultdetailpage> createState() => _KalResultdetailpageState();
}

class _KalResultdetailpageState extends State<KalResultdetailpage> {
  var _datas = [];
  var _firstPrize_numbersMap = {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    List _prizes = widget.prizes;

    ///1st
    List balls = [];
    PrizeModel prizeModel = _prizes[0];
    List ticketNumbers = prizeModel.ticket_numbers!;
    if (ticketNumbers.isNotEmpty) {
      String numberStr = ticketNumbers[0];
      // // 使用 split 方法将字符串转换为字符串数组
      List<String> charArray = numberStr.split('');
      // numbersMap = charArray;
      for (var i = 0; i < charArray.length; i++) {
        var ball = {
          'number': charArray[i],
          'isRight': false,
          'isNormal': true
        }; //isNormal代表是否对比中奖号码，true不用对比，false需要对比。isRight代表是否中奖号码
        balls.add(ball);
      }
      var title = prizeModel.rank_title + ' Prize';
      _firstPrize_numbersMap = {
        'prizeAmount': prizeModel.prize_amount,
        'title': title,
        'balls': balls
      };
    }

    ///
    for (var i = 0; i < _prizes.length - 1; i++) {
      PrizeModel prizeModel = _prizes[i + 1];
      List ticketNumbers = prizeModel.ticket_numbers!;
      if (ticketNumbers.isNotEmpty) {
        var title = prizeModel.rank_title + ' Prize';
        Map ball = {
          'prizeAmount': prizeModel.prize_amount,
          'title': title,
          'number': ticketNumbers,
        };
        _datas.add(ball);
      }
    }

    debugPrint('datas $_datas');

    // _datas = [
    //   [
    //     '649722',
    //   ],
    //   [
    //     '649722',
    //     '649723',
    //     '649723',
    //   ],
    //   [
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //   ],
    //   [
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //     '649724',
    //     '649722',
    //     '649723',
    //     '649724'
    //   ]
    // ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(
            'Result Details',
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18,
            ),
          ),
          backgroundColor: KaL_Config.KaL_MainColor,
          foregroundColor: Colors.white,
          centerTitle: true,
          elevation: 0,
        ),
        body: Container(
          color: Colors.white,
          child: ListView.builder(
              itemCount: _datas.length + 1,
              itemBuilder: (context, index) {
                if (index == 0) {
                  return KalDrawnumbersview(data: _firstPrize_numbersMap);
                }

                var numberMap = _datas[index - 1];
                return Kal_ResultDetaillistcell(
                  numberMap: numberMap,
                );
              }),
        ));
  }
}
